<template>
  <a-modal v-model="visible" :mask-closable="false" width="80%" title="查看" :dialog-style="{ top: '20px' }" :footer="null">
    <div v-if="visible" v-permission="['base-data:product:info:query']" v-loading="loading">
      <a-descriptions :column="4" bordered size="small">
        <a-descriptions-item label="商品编号" :span="2">{{ formData.code }}</a-descriptions-item>
        <a-descriptions-item label="商品名称" :span="2">{{ formData.name }}</a-descriptions-item>
        <a-descriptions-item label="类目" :span="2">{{ formData.categoryName }}</a-descriptions-item>
        <a-descriptions-item label="商品简称" :span="2">{{ formData.shortName }}</a-descriptions-item>
        <!--        <a-descriptions-item label="SKU编号" :span="2">{{ formData.skuCode }}</a-descriptions-item>-->
        <!--        <a-descriptions-item label="外部编号" :span="2">{{ formData.externalCode }}</a-descriptions-item>-->
        <a-descriptions-item label="品牌" :span="2">{{ formData.brandName }}</a-descriptions-item>
        <a-descriptions-item label="规格" :span="2">{{ formData.spec }}</a-descriptions-item>
<!--        <a-descriptions-item label="单位" :span="2">{{ formData.unit }}</a-descriptions-item>-->
        <a-descriptions-item label="重量（kg）" :span="2">{{ $enums.PRODUCT_TYPE.NORMAL.equalsCode(formData.productType) ? formData.weight : '-' }}</a-descriptions-item>
        <a-descriptions-item label="体积（cm³）" :span="2">{{ $enums.PRODUCT_TYPE.NORMAL.equalsCode(formData.productType) ? formData.volume : '-' }}</a-descriptions-item>
<!--        <a-descriptions-item label="进项税率（%）" :span="2">{{ $enums.PRODUCT_TYPE.NORMAL.equalsCode(formData.productType) ? formData.taxRate : '-' }}</a-descriptions-item>-->
<!--        <a-descriptions-item label="销项税率（%）" :span="2">{{ $enums.PRODUCT_TYPE.NORMAL.equalsCode(formData.productType) ? formData.saleTaxRate : '-' }}</a-descriptions-item>-->
<!--        <a-descriptions-item label="采购价（元）" :span="2">{{ $enums.PRODUCT_TYPE.NORMAL.equalsCode(formData.productType) ? formData.purchasePrice : '-' }}</a-descriptions-item>-->
<!--        <a-descriptions-item label="销售价（元）" :span="2">{{ formData.salePrice }}</a-descriptions-item>-->
<!--        <a-descriptions-item label="零售价（元）" :span="2">{{ formData.retailPrice }}</a-descriptions-item>-->
<!--        <a-descriptions-item label="商品类型" :span="4">{{ $enums.PRODUCT_TYPE.getDesc(formData.productType) }}</a-descriptions-item>-->
        <a-descriptions-item label="状态" :span="4"><available-tag :available="formData.available" /></a-descriptions-item>
        <a-descriptions-item v-for="item in formData.properties" :key="item.id" :label="item.name" :span="4">
          {{ item.textStr }}
        </a-descriptions-item>
      </a-descriptions>
    </div>
    <a-row>
      <a-col :span="24">
        <vxe-grid
          ref="grid"
          resizable
          show-overflow
          highlight-hover-row
          keep-source
          row-id="id"
          height="360"
          :data="productSkus"
          :columns="[
                // { type: 'checkbox', width: 40 },
                { field: 'id', title: '商品SKUID', width: 200, align: 'center', visible : false},
                { field: 'skuCode', title: 'SKU编号', width: 120, align: 'center', slots: { default: 'product_sku_skuCode_default' ,header: 'product_sku_skuCode_header'}},
                { field: 'externalCode', title: '外部编号', width: 120, align: 'center', slots: { default: 'product_sku_externalCode_default' }},
                { field: 'skuPropertys', title: '多属性ID值', width: 200, align: 'center', slots: { default: 'product_sku_propertys_default' }, visible : false},
                { field: 'skuPropertysStr', title: '多属性', width: 200, align: 'center', slots: { default: 'product_sku_propertys_str_default',header: 'product_sku_propertys_header' }},
                { field: 'unit', title: '单位', width: 80, align: 'center', slots: { default: 'product_sku_unit_default' }},
                { field: 'purchasePrice', title: '采购价', width: 100, align: 'center', slots: { default: 'product_sku_purchasePrice_default', header: 'product_sku_purchasePrice_header' }},
                { field: 'salePrice', title: '销售价', width: 100, align: 'center', slots: { default: 'product_sku_salePrice_default' ,header: 'product_sku_salePrice_header'}},
                { field: 'retailPrice', title: '零售价', width: 100, align: 'center', slots: { default: 'product_sku_retailPrice_default' ,header: 'product_sku_retailPrice_header'}},
                { field: 'taxRate', title: '进项税率%', width: 120, align: 'center', slots: { default: 'product_sku_taxRate_default' }},
                { field: 'saleTaxRate', title: '销项税率%', width: 120, align: 'center', slots: { default: 'product_sku_saleTaxRate_default' }},
                { field: 'available', title: '状态', width: 100, align: 'center', slots: { default: 'product_sku_available_default' }}
              ]"
          :toolbar-config="{
                // 缩放
                zoom: false,
                // 自定义表头
                custom: false,
                // 右侧是否显示刷新按钮
                refresh: false
              }"
        >

          <!-- SKU编号 列自定义表头 -->
          <template v-slot:product_sku_skuCode_header>
            <a-space>
              <span>SKU编号<span style="color: red;padding: 2px 0px 0px 2px;">*</span></span>
            </a-space>
          </template>

          <!-- SKU编号 列自定义内容 -->
          <template v-slot:product_sku_skuCode_default="{ row }">
            <span>{{row.skuCode}}</span>
          </template>

          <!-- SKU外部编号 列自定义内容 -->
          <template v-slot:product_sku_externalCode_default="{ row }">
            <span>{{row.externalCode}}</span>
          </template>

          <!-- SKU属性 列自定义表头 -->
          <template v-slot:product_sku_propertys_header>
            <a-space>
              <span>多属性<span style="color: red;padding: 2px 0px 0px 2px;">*</span></span>
            </a-space>
          </template>

          <!-- 多属性ID值 列自定义内容 -->
          <template v-slot:product_sku_propertys_default="{ row }">
            <span>{{row.skuPropertys}}</span>
          </template>

          <!-- 多属性 列自定义内容 -->
          <template v-slot:product_sku_propertys_str_default="{ row }">
            <span>{{row.skuPropertysStr}}</span>
          </template>

          <!-- SKU单位 列自定义内容 -->
          <template v-slot:product_sku_unit_default="{ row }">
            <span>{{row.unit}}</span>
          </template>

          <!-- SKU采购价 列自定义表头 -->
          <template v-slot:product_sku_purchasePrice_header>
            <a-space>
              <span>采购价<span style="color: red;padding: 2px 0px 0px 2px;">*</span></span>
            </a-space>
          </template>

          <!-- SKU采购价 列自定义内容 -->
          <template v-slot:product_sku_purchasePrice_default="{ row }">
            <span>{{row.purchasePrice}}</span>
          </template>

          <!-- 进项税率% 列自定义内容 -->
          <template v-slot:product_sku_taxRate_default="{ row }">
            <span>{{row.taxRate}}</span>
          </template>

          <!-- SKU销售价 列自定义表头 -->
          <template v-slot:product_sku_salePrice_header>
            <a-space>
              <span>销售价<span style="color: red;padding: 2px 0px 0px 2px;">*</span></span>
            </a-space>
          </template>

          <!-- SKU销售价 列自定义内容 -->
          <template v-slot:product_sku_salePrice_default="{ row }">
            <span>{{row.salePrice}}</span>
          </template>

          <!-- 销项税率% 列自定义内容 -->
          <template v-slot:product_sku_saleTaxRate_default="{ row }">
            <span>{{row.saleTaxRate}}</span>
          </template>

          <!-- SKU零售价 列自定义表头 -->
          <template v-slot:product_sku_retailPrice_header>
            <a-space>
              <span>零售价<span style="color: red;padding: 2px 0px 0px 2px;">*</span></span>
            </a-space>
          </template>

          <!-- SKU零售价 列自定义内容 -->
          <template v-slot:product_sku_retailPrice_default="{ row }">
            <span>{{row.retailPrice}}</span>
          </template>

          <!-- SKU状态 列自定义内容 -->
          <template v-slot:product_sku_available_default="{ row }">
            <available-tag :available="row.available" />
          </template>
        </vxe-grid>
      </a-col>
    </a-row>
  </a-modal>
</template>
<script>
import AvailableTag from '@/components/Tag/Available'
export default {
  // 使用组件
  components: {
    AvailableTag
  },

  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 是否可见
      visible: false,
      // 是否显示加载框
      loading: false,
      // 商品SKU集合
      productSkus: [],
      // 表单数据
      formData: {}
    }
  },
  created() {
    this.initFormData()
  },
  methods: {
    // 打开对话框 由父页面触发
    openDialog() {
      this.visible = true

      this.$nextTick(() => this.open())
    },
    // 关闭对话框
    closeDialog() {
      this.visible = false
      this.$emit('close')
    },
    // 初始化表单数据
    initFormData() {
      this.formData = {
        id: '',
        code: '',
        name: '',
        skuCode: '',
        externalCode: '',
        categoryName: '',
        brandName: '',
        spec: '',
        unit: '',
        purchasePrice: '',
        salePrice: '',
        retailPrice: '',
        available: '',
        properties: []
      }
    },
    // 页面显示时触发
    open() {
      // 初始化数据
      this.initFormData()

      // 查询数据
      this.loadFormData()
    },
    // 查询数据
    async loadFormData() {
      this.loading = true
      this.selectSkus()
      await this.$api.baseData.product.spu.get(this.id).then(data => {
        this.formData = data
      }).finally(() => {
        this.loading = false
      })
    },
    // 查询商品SKU数据
    selectSkus() {
      const params = {
        spuId: this.id
      }
      this.$api.baseData.product.info.queryList(params).then(res => {
        this.productSkus = res
      })
    }
  }
}
</script>
